package com.ccn.qrcode.utils.qrcodefile;

import com.ccn.qrcode.pojo.MatrixCellPojo;
import com.google.common.collect.Lists;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.encoder.ByteMatrix;

import java.util.List;

/**
 * 二维码 矩阵解析
 * @author tig
 */
public class BitMatrixUtils {

    public List<MatrixCellPojo> embedDataBits(BitMatrix matrix, ByteMatrix tmpMatrix) {
        List<MatrixCellPojo> pojoList = Lists.newArrayList();
        int bitIndex = 0;
        int direction = -1;
        // Start from the right bottom cell.
        int x = matrix.getWidth() - 1;
        int y = matrix.getHeight() - 1;
        while (x > 0) {
            // Skip the vertical timing pattern.
            if (x == 6) {
                x -= 1;
            }
            while (y >= 0 && y < matrix.getHeight()) {
                for (int i = 0; i < 2; ++i) {
                    int xx = x - i;
                    // Skip the cell if it's not empty.
                    if (!isEmpty(tmpMatrix.get(xx, y))) {
                        continue;
                    }
                    MatrixCellPojo matrixCellPojo = new MatrixCellPojo();
                    matrixCellPojo.setX(xx);
                    matrixCellPojo.setY(y);
                    matrixCellPojo.setValue(matrix.get(xx, y)? 1: 0);
                    pojoList.add(matrixCellPojo);
                }
                y += direction;
            }
            direction = -direction;  // Reverse the direction.
            y += direction;
            x -= 2;  // Move to the left.
        }
        return pojoList;
    }

    private static boolean isEmpty(int value) {
        return value == -1;
    }


    public void clearMatrix(ByteMatrix matrix) {
        matrix.clear((byte) -1);
    }

    public void embedBasicPatterns(String qrVersion, ByteMatrix matrix) {
    }
}
